﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace physio.assist
{
    /// <summary>
    /// Interaction logic for Agenda.xaml
    /// </summary>
    public partial class Agenda : Window
    {






        //Inicialização da Janela
        public Agenda()
        {
            InitializeComponent();



            //Ajusta posição e tamanho das janelas
            double width = System.Windows.SystemParameters.PrimaryScreenWidth;
            double height = System.Windows.SystemParameters.PrimaryScreenHeight;

            winAgenda.Width = width;
            winAgenda.Height = height;

            winAgenda.Left = 0;
            winAgenda.Top = 0;



            //Ajusta posicionamento e tamanho dos panels
            pnlGeral.Width = width;
            pnlGeral.Height = height;

            pnlVoltar.Height = height;
            pnlPrimaria.Height = height;
            pnlSecundaria.Height = height;

            pnlVoltar.Width = width * 0.2;
            pnlPrimaria.Width = width * 0.5;
            pnlSecundaria.Width = width * 0.3;




            //Lista Compromissos PRIMARIA
            int profissional = 1;//hardcode
            int dia = DateTime.Now.Day;
            int mes = DateTime.Now.Month;
            int ano = DateTime.Now.Year;

            PhysioAssistWS.PhysioAssistSoapClient ws = new PhysioAssistWS.PhysioAssistSoapClient();
            foreach (PhysioAssistWS.Agenda agenda in ws.ListaAgenda(profissional, dia, mes, ano))
            {
                if (agenda.Compromisso == "Almoço")//Quando for almoço, adiciona dois espaços
                { ListaCompromissos("", "", pnlPrimaria, Brushes.White, Brushes.Black, 40); ListaCompromissos(agenda.Hora, agenda.Compromisso, pnlPrimaria, Brushes.White, Brushes.Black, 40); ListaCompromissos("", "", pnlPrimaria, Brushes.White, Brushes.Black, 40); }
                else ListaCompromissos(agenda.Hora, agenda.Compromisso, pnlPrimaria, Brushes.White, Brushes.Black, 40);
            }





            //Lista Compromissos SECUNDARIA
            profissional = 1;//hardcode
            dia = DateTime.Now.AddDays(1).Day;
            mes = DateTime.Now.AddDays(1).Month;
            ano = DateTime.Now.AddDays(1).Year;

            ws = new PhysioAssistWS.PhysioAssistSoapClient();
            foreach (PhysioAssistWS.Agenda agenda in ws.ListaAgenda(profissional, dia, mes, ano))
            {
                if (agenda.Compromisso == "Almoço")//Quando for almoço, adiciona dois espaços
                { ListaCompromissos("", "", pnlSecundaria, Brushes.Silver, Brushes.Black, 30); ListaCompromissos(agenda.Hora, agenda.Compromisso, pnlSecundaria, Brushes.Silver, Brushes.Black, 30); ListaCompromissos("", "", pnlSecundaria, Brushes.Silver, Brushes.Black, 30); }
                else ListaCompromissos(agenda.Hora, agenda.Compromisso, pnlSecundaria, Brushes.Silver, Brushes.Black, 30);
            }




        }






        //AGENDA
        private void ListaCompromissos(string hora, string compromisso, StackPanel panel, Brush corHora, Brush corCompromisso, int tamanhoFonte)
        {

            //Cria stack horizontal
            StackPanel pnl = new StackPanel();
            pnl.Orientation = Orientation.Horizontal;

            //Cria label hora
            Label lblHora = new Label();
            BrushConverter bc = new BrushConverter();
            lblHora.FontSize = tamanhoFonte;
            lblHora.Foreground = corHora;
            lblHora.Content = hora;

            //Cria label compromisso
            Label lblCompromisso = new Label();
            lblCompromisso.FontSize = tamanhoFonte;
            lblCompromisso.Foreground = corCompromisso;
            lblCompromisso.Content = compromisso;

            //Insere labels no panel
            pnl.Children.Add(lblHora);
            pnl.Children.Add(lblCompromisso);

            //Insere panel no panel principal
            panel.Children.Add(pnl);


        }







        //RECONHECIMENTO DE VOZ
        private Microsoft.Speech.Recognition.SpeechRecognitionEngine speechEngine;
        private Microsoft.Kinect.KinectSensor sensor;
        private void winAgenda_Loaded(object sender, RoutedEventArgs e)
        {


            //Obtem o sensor ativo
            foreach (var potentialSensor in Microsoft.Kinect.KinectSensor.KinectSensors)
            {
                if (potentialSensor.Status == Microsoft.Kinect.KinectStatus.Connected)
                {
                    sensor = potentialSensor;
                    break;
                }
            }


            if (null != sensor)
            {

                try
                {
                    sensor.Start();
                }
                catch (System.IO.IOException)
                {
                    sensor = null;
                }
            }








            //RECONHECIMENTO DE VOZ
            Microsoft.Speech.Recognition.RecognizerInfo ri = GetKinectRecognizer();

            if (null != ri)
            {

                this.speechEngine = new Microsoft.Speech.Recognition.SpeechRecognitionEngine(ri.Id);

                // Create a grammar from grammar definition XML file.
                using (var memoryStream = new System.IO.MemoryStream(Encoding.ASCII.GetBytes(Properties.Resources.PhysioAssistGrammar)))
                {
                    var g = new Microsoft.Speech.Recognition.Grammar(memoryStream);
                    speechEngine.LoadGrammar(g);
                }

                speechEngine.SpeechRecognized += SpeechRecognized;

                speechEngine.SetInputToAudioStream(sensor.AudioSource.Start(), new Microsoft.Speech.AudioFormat.SpeechAudioFormatInfo(Microsoft.Speech.AudioFormat.EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
                speechEngine.RecognizeAsync(Microsoft.Speech.Recognition.RecognizeMode.Multiple);
            }


        }
        private static Microsoft.Speech.Recognition.RecognizerInfo GetKinectRecognizer()
        {
            foreach (Microsoft.Speech.Recognition.RecognizerInfo recognizer in Microsoft.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers())
            {
                string value;
                recognizer.AdditionalInfo.TryGetValue("Kinect", out value);
                if ("True".Equals(value, StringComparison.OrdinalIgnoreCase) && "en-US".Equals(recognizer.Culture.Name, StringComparison.OrdinalIgnoreCase))
                {
                    return recognizer;
                }
            }

            return null;
        }
        private void SpeechRecognized(object sender, Microsoft.Speech.Recognition.SpeechRecognizedEventArgs e)
        {
            // Speech utterance confidence below which we treat speech as if it hadn't been heard
            const double ConfidenceThreshold = 0.3;

            if (e.Result.Confidence >= ConfidenceThreshold)
            {
                switch (e.Result.Semantics.Value.ToString())
                {
                    case "CLOSE":
                        winAgenda.Close();
                        break;

                    case "FECHAR":
                        winAgenda.Close();
                        break;
                }
            }
        }










    }
}
